import { setToken, removeToken } from "@/utils/auth";
import { setStore, clearStores } from "@/utils/store";
import { userLogin } from '@/api/login';

const login = {
  state: {
    token: '',
    userInfo: {}
  },
  actions: {
    // 手机号登录
    userphoneLogin({ commit },userInfo) {
      
      return new Promise((resolve, reject) => {
        userLogin(userInfo.phone,userInfo.password).then(res => {
          console.log(res);
          commit("SET_TOKEN", res.token);
          resolve();
        })
        .catch(err => {
          reject(err);
        });
      })
    },
    //注销session
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '');
        commit('SET_ROLES', []);
        commit('CLEAR_LOCK');
        removeToken();
        clearStores();
        resolve();
      })
    }
  },
  mutations: {
    SET_TOKEN: (state, token) => {
      setToken(token)
      state.token = token;
      setStore({ name: 'token', content: state.token, type: 'session' })
    }
  }
}

export default login;